package com.shangdiao.sd_server.util;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Random;

import org.apache.commons.io.IOUtils;

public class CommonUtil {
	/**
	 * 生成6位短信验证码
	 * 
	 * @return
	 */
	public static String genAuth_code() {
		String auth_code = String.valueOf(System.currentTimeMillis());
		return auth_code.substring(7, 13);
	}
	
    public static String genRandomPwd(int length) {
        Random rnd = new Random(System.currentTimeMillis());
        int letterLength = length - 1;
        byte[] buf = new byte[letterLength];
        for (int i = 0; i < letterLength; ++i) {
            int idx = Math.abs(rnd.nextInt()) % 26;
            int offset = idx % 2 == 1 ? 65 : 97;
            int ch = offset + idx;
            buf[i] = (byte) ch;
        }
        return insertRandomNumber(buf);
 
    }
    private static String insertRandomNumber(byte[] buf) {
        StringBuffer sb = new StringBuffer(new String(buf));
        Random rnd = new Random(System.currentTimeMillis());
        int number = Math.abs(rnd.nextInt()) % 9;
        int offset = Math.abs(rnd.nextInt()) % (sb.length());
        sb.insert(offset, number);
        return sb.toString();
    }

	/**
	 * 移动文件
	 * 
	 * @param srcFile
	 * @param destPath
	 * @return
	 */
	public static boolean Move(String srcFile, String destPath) {
		try {
			int byteread = 0;
			File oldfile = new File(srcFile);
			
			if (oldfile.exists()) { // 原文件存在时 
				InputStream inStream = new FileInputStream(srcFile); // 读入原文件
				FileOutputStream fs = new FileOutputStream(destPath);
				byte[] buffer = new byte[1444];
				while ((byteread = inStream.read(buffer)) != -1) {
					fs.write(buffer, 0, byteread);
				}
				File destFile = new File(destPath);
				destFile.setReadable(true);
				destFile.setExecutable(true);
				destFile.setWritable(true);
				inStream.close();
				return true;
			}else{
				return false;
			}
		} catch (Exception e) {
			System.out.println("复制单个文件操作出错");
			e.printStackTrace();
			return false;
		}
	}
	
	
	/**
	 * 批量移动文件
	 * 
	 * @param srcFile
	 * @param destPath
	 * @return
	 */
	public static boolean MoveMore(String[] srcFileArr, String[] destPathArr) {
		try {
			if(srcFileArr.length != destPathArr.length){
				return false;
			}
			for(int i = 0 ; i < srcFileArr.length; i ++){
				String srcFile = srcFileArr[i];
				String destPath = destPathArr[i];
				int byteread = 0;
				File oldfile = new File(srcFile);
				if (oldfile.exists()) { // 文件存在时
					InputStream inStream = new FileInputStream(srcFile); // 读入原文件
					FileOutputStream fs = new FileOutputStream(destPath);
					byte[] buffer = new byte[1444];
					while ((byteread = inStream.read(buffer)) != -1) {
						fs.write(buffer, 0, byteread);
					}
					File destFile = new File(destPath);
					destFile.setReadable(true);
					destFile.setExecutable(true);
					destFile.setWritable(true);
					inStream.close();
				}
			}
			return true;
		} catch (Exception e) {
			System.out.println("复制多个文件操作出错");
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean deleteFile(String filePath){
		try{
			File file = new File(filePath);
			file.delete();
			return true;
		} catch (Exception e) {
			System.out.println("删除单个文件操作出错");
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean deleteMoreFile(List<String> filePathList){
		try{
			for(String filePath : filePathList){
				File file = new File(filePath);
				file.delete();
			}
			return true;
		} catch (Exception e) {
			System.out.println("删除单个文件操作出错");
			e.printStackTrace();
			return false;
		}
	}
	
	public static String doHttpPost(String xmlInfo,String URL){         
        System.out.println("发起的数据:"+xmlInfo);       
       byte[] xmlData = xmlInfo.getBytes();            
        InputStream instr  = null; 
        java.io.ByteArrayOutputStream out = null;              
         try{                          
                URL url = new URL(URL);                
                URLConnection urlCon = url.openConnection();               
                urlCon.setDoOutput(true);              
                urlCon.setDoInput(true);               
                urlCon.setUseCaches(false);                            
                urlCon.setRequestProperty("Content-Type", "text/xml");       
                urlCon.setRequestProperty("Content-length",String.valueOf(xmlData.length)); 
                System.out.println(String.valueOf(xmlData.length));
                DataOutputStream printout = new DataOutputStream(urlCon.getOutputStream());            
                printout.write(xmlData);               
                printout.flush();              
                printout.close();              
                instr = urlCon.getInputStream();   
                byte[] bis = IOUtils.toByteArray(instr);
                String ResponseString = new String(bis, "UTF-8");  
               if ((ResponseString == null) || ("".equals(ResponseString.trim()))) {
                    System.out.println("返回空");
                   }
              System.out.println("返回数据为:" + ResponseString);
             return ResponseString;    
            
         }catch(Exception e){              
                e.printStackTrace();
               return "0";
         }             
         finally {             
                try {          
                       out.close();   
                       instr.close();  
                        
                }catch (Exception ex) {      
                       return "0";
                    }                  
                }                  
         }    
	
	public static void main(String[] args) {
		System.out.println(genRandomPwd(8));
		
	}

	
	
}
